﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using BGM.Domain.DTO;


namespace BGM.Application.View
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    internal partial class PieUserControl : UserControl
    {
        public PieUserControl(Patient patient, Dictionary<string, double> categories)
        {
            InitializeComponent();
            Pie pie = new Pie();
            pie.Radius = 200;
            pie.StartPie();
            canv.Children.Clear();
            foreach (var item in categories)
            {
                Path p = pie.GetPiePice(item.Value, item.Key);
                Point point = (Point)p.Tag;
                p.Tag = item;
                p.Fill = GetBrush(p);
                Label l = new Label();
                l.Content = item.Key + " { " + item.Value + " }";
                l.Margin = new Thickness(point.X, point.Y, 0, 0);
                p.MouseEnter += new MouseEventHandler(p_MouseEnter);
                p.MouseLeave += new MouseEventHandler(p_MouseLeave);
                p.MouseDown += new MouseButtonEventHandler(p_MouseDown);
                p.Opacity = 50;
                l.Focusable = false;
               
                l.Width = 80;
                l.Height = 25;
                canv.Children.Add(p);
                canv.Children.Add(l);
                
            }

            int textOffset = 50;
            Label patientLabel = new Label();
            patientLabel.Content = patient.Name + " - " + patient.Ssn;
            Canvas.SetLeft(patientLabel, pie.Radius*2+textOffset);
            canv.Children.Add(patientLabel);

            Label high = new Label();
            high.Content = "High value: " + patient.LevelIndicator.High;
            Canvas.SetLeft(high, pie.Radius * 2 + textOffset);
            Canvas.SetTop(high, 20);
            canv.Children.Add(high);

            Label low = new Label();
            low.Content = "Low  value: " + patient.LevelIndicator.Low;
            Canvas.SetLeft(low, pie.Radius * 2 + textOffset);
            Canvas.SetTop(low, 40);
            canv.Children.Add(low);



        }

        void p_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as Path;
            item.Fill = Brushes.CadetBlue;
        }

        void p_MouseLeave(object sender, MouseEventArgs e)
        {
            var item = sender as Path;
            item.Fill = GetBrush(item);
        }
        private Brush GetBrush(Path p)
        {
            var item = (KeyValuePair<string, double>)p.Tag;
            if (item.Key.Contains("Low"))
                return Brushes.Blue;
            else if (item.Key.Contains("Ok"))
                return Brushes.LightGreen;
            else if (item.Key.Contains("High"))
                return Brushes.Red;
            return Brushes.White;
        }

        void p_MouseEnter(object sender, MouseEventArgs e)
        {
            var item = sender as Path;
            item.Fill = Brushes.WhiteSmoke;
        }
    }
}
